// pages/dateChoose/dateChoose.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateArr: [
      {
        name: '周一',
        val: 1
      },
      {
        name: '周二',
        val: 2
      },
      {
        name: '周三',
        val: 3
      },
      {
        name: '周四',
        val: 4,
      },
      {
        name: '周五',
        val: 5
      },
      {
        name: '周六',
        val: 6
      },
      {
        name: '周日',
        val: 7
      }
    ],
    type: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  choose(e) {
    const index = e.currentTarget.dataset.index
    if (!this.data.dateArr[index].active) {
      if (this.data.type == 'no') {
        if (this.data.dateArr[index].val == -1) {
          this.data.dateArr.forEach(val => { val.active = false })
        }
        this.data.dateArr[0].active = false
      }

      this.data.dateArr[index].active = true
    }
    else {
      this.data.dateArr[index].active = false
    }
    this.setData({
      dateArr: this.data.dateArr
    })
  },
  onLoad: function (options) {
    //   let deliver = JSON.parse(options.deliver)
    if (options.type == 'no') {
      this.data.dateArr.unshift({ name: '无', val: -1 })
      this.setData({
        dateArr: this.data.dateArr,
        type: options.type
      })
    }
    console.log(options)
    this.data.dateArr.filter(val => {
      let date = options.date.split(',')
      // let include = false
      return date.filter(date => date == val.val).length
      // if (date.includes(val.val)) {
      //   return true
      // }
    }).forEach(val => {
      val.active = true
    })

    this.setData({
      // deliver,
      dateArr: this.data.dateArr
    })
  },
  save() {
    let activeArr = this.data.dateArr.filter(val => val.active)
    if (!activeArr.length) {
      wx.showModal({
        title: '提示',
        content: '请点击至少选择一个时间点',
        showCancel: false
      })
    }
    else {
      let settle = activeArr.map(val => val.val)
      let result = settle.join(',')
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setDate(result)
      wx.navigateBack({})
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})